package com.xposed.openfire.smack;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.xposed.openfire.smack.xmpp.XmppConnection;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserLogonActivity extends AppCompatActivity {

    @BindView(R.id.edit_user_name)
    TextInputEditText editUserName;
    @BindView(R.id.edit_password)
    TextInputEditText editPassword;
    @BindView(R.id.edit_password_sure)
    TextInputEditText editPasswordSure;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_logon);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_user_logon})
    public void onViewClicked(View view) {

        String userName = editUserName.getText().toString();
        String password = editPassword.getText().toString();
        String passwordSure = editPasswordSure.getText().toString();

        if (TextUtils.isEmpty(userName)) {
            Toast.makeText((this), ("请输入用户名"), Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(password)) {
            Toast.makeText((this), ("请输入密码"), Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(passwordSure) || !password.equals(passwordSure)) {
            Toast.makeText((this), ("确认密码错误"), Toast.LENGTH_SHORT).show();
            return;
        }

        Map<String, String> map = new HashMap<>();
        map.put("name", userName);
        map.put("email", userName + "@example.com");
        boolean isLogon = XmppConnection.getInstance().userLogon(userName, password, map);

        if (isLogon) {
            Toast.makeText((this), ("注册成功"), Toast.LENGTH_SHORT).show();
            finish();
        } else {
            Toast.makeText((this), ("注册失败"), Toast.LENGTH_SHORT).show();
        }
    }

}
